import 'package:flutter/material.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/stock_out_order_goods_detail_entity.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:module_wms/page/outbound_orders/as_order/order_detail/inventory_distribution/base_inventory_distribution_single_controller.dart';
import 'package:module_wms/page/outbound_orders/as_order/order_detail/inventory_distribution/base_inventory_distribution_page_ui.dart';

abstract class BaseInventoryDistributionPageState<T extends StatefulWidget,
    C extends BaseInventoryDistributionSingleController> extends BaseState<T, C> {
  late List<String> titleList;

  late String goodsName;

  late String _title;

  bool singleEntry = false;

  bool isShowShadow = false;

  double planOutCount = 0;

  ///是否可分配商品,如果是已完成的商品不可再进行分配
  bool enablePreDistributedGoods = true;

  final ScrollController _titleController = ScrollController();
  final ScrollController _contentController = ScrollController();

  @override
  void onCreate() {
    super.onCreate();
    var map = Get.arguments;
    titleList = getTitleList();
    _title = map['title'];
    enablePreDistributedGoods = map['enablePreDistributedGoods'];
    goodsName = map['goodsName'];
    singleEntry = map['singleEntry'];
    planOutCount = map['planOutCount'];
    _titleController.addListener(_updateContent);
    _contentController.addListener(_updateTitle);
    setNavigationBarColor(ThemeUtil.getTheme().colorScheme.secondary);
    setTitle(enablePreDistributedGoods?MultiLanguage.inventoryDistribution.tr:MultiLanguage.inventoryDistributionDetails.tr);
  }

  void _updateTitle() {
    if (_titleController.offset != _contentController.offset) {
      _titleController.jumpTo(_contentController.offset);
    }
    if(isShowShadow!=(_titleController.offset!=0)){
      isShowShadow = _titleController.offset!=0;
      setState(() {});
    }
  }

  void _updateContent() {
    if (_contentController.offset != _titleController.offset) {
      _contentController.jumpTo(_titleController.offset);
    }
  }

  @override
  void onDestroy() {
    _titleController.removeListener(_updateContent);
    _contentController.removeListener(_updateTitle);
    super.onDestroy();
  }

  @override
  Widget? getMainWidget(BuildContext context) {
    return Column(
      children: [
        Container(
          color: ThemeUtil.getTheme().secondaryBgColor(),
          child: Row(
            children: [
              Container(
                decoration: isShowShadow?BoxDecoration(
                  color: ThemeUtil.getTheme().secondaryBgColor(),
                    boxShadow: const [
                      BoxShadow(
                          offset: Offset(8.0, 0.0), //阴影xy轴偏移量
                          blurRadius: 10.0, //阴影模糊程度
                          spreadRadius: -15, //阴影扩散程度
                          blurStyle: BlurStyle.outer
                      )
                    ],
                ):BoxDecoration(
                  color: ThemeUtil.getTheme().secondaryBgColor(),
                ),
                width: getLeftTitleWidth(),
                height: getCellHeight(),
                alignment: Alignment.center,
                child: Text(_title, style: normalTextColor26),
              ),
              Expanded(
                  child: SizedBox(
                width: singleEntry ? 600.w : 550.w,
                height: getCellHeight(),
                child: SingleChildScrollView(
                  controller: _titleController,
                  scrollDirection: Axis.horizontal,
                  child: Container(
                    child: Row(
                      children: _buildRightTitle(),
                    ),
                  ),
                ),
              ))
            ],
          ),
        ),
        Expanded(
          child: SingleChildScrollView(
          child: Column(
            children: [
              Row(
                children: [
                  SizedBox(
                    width: getLeftTitleWidth(),
                    child: MediaQuery.removePadding(
                      context: context,
                      removeTop: true,
                      removeBottom: true,
                      child: ListView.builder(
                        shrinkWrap: true,
                        physics: const NeverScrollableScrollPhysics(),
                        itemCount: baseGetxController!.goodsDetails.length,
                        itemBuilder: (context, index) {
                          return GestureDetector(
                              onTap: () {
                                if(!enablePreDistributedGoods){
                                  return;
                                }
                                setState(() {
                                  var isSelectedItems = baseGetxController!.isSelectedData.where((element) => element).toList();

                                  if(!baseGetxController!.isSelectedData[index] && isSelectedItems.length>=planOutCount){
                                    ToastUtil.normal(MultiLanguage.no_more.tr);
                                    return;
                                  }

                                  baseGetxController!.isSelectedData[index] = !baseGetxController!.isSelectedData[index];
                                  if(baseGetxController!.checkedStockOutOrderGoodsDetailEntity.contains(baseGetxController!.goodsDetails[index])){
                                    baseGetxController!.checkedStockOutOrderGoodsDetailEntity.remove(baseGetxController!.goodsDetails[index]);
                                  }else{
                                    baseGetxController!.checkedStockOutOrderGoodsDetailEntity.add(baseGetxController!.goodsDetails[index]);
                                  }
                                });
                              },
                              child: Container(
                                alignment: const Alignment(0, 0),
                                decoration: isShowShadow?rectRight:BoxDecoration(
                                    border: Border(
                                      bottom: BorderSide(
                                          color: ThemeUtil.getTheme()
                                              .dividingLineColor(),
                                          width: 0.7),
                                    )),
                                width: getLeftTitleWidth(),
                                height: getCellHeight(),
                                child: LocalImage(
                                    baseGetxController!.isSelectedData[index]
                                        ? 'wms_round_√'
                                        : 'wms_round_no_√',
                                    package: Constant.packWms(),
                                    width: 34.w,
                                    height: 34.w),
                              ));
                        },
                      ),
                    ),
                  ),
                  Expanded(
                      child: SingleChildScrollView(
                    controller: _contentController,
                    scrollDirection: Axis.horizontal,
                    child: SizedBox(
                      width: getRightRowWidth(),
                      child: MediaQuery.removePadding(
                        context: context,
                        removeTop: true,
                        removeBottom: true,
                        child: ListView.builder(
                          itemCount: baseGetxController!.goodsDetails.length,
                          shrinkWrap: true,
                          physics: const NeverScrollableScrollPhysics(),
                          itemBuilder: (context, index) {
                            return GestureDetector(
                              onTap: () {},
                              child: Row(
                                children: buildRightContent(index),
                              ),
                            );
                          },
                        ),
                      ),
                      // ),
                    ),
                  ))
                ],
              ),
            ],
          ),
        )),
        Visibility(
            visible: enablePreDistributedGoods,
            child: buildBottom(distribute:baseGetxController!.distribute)),
      ],
    );
  }

  Container _buildTitleContainer(int i) {
    return Container(
      // color: ThemeUtil.getTheme().secondaryBgColor(),
      alignment: Alignment.center,
      width: getTitleWidth(i),
      height: getCellHeight(),
      child: Text(
        titleList[i],
        style: normalTextColor26,
      ),
    );
  }

  double getLeftTitleWidth();

  double getTitleWidth(int i);

  double getRightRowWidth();

  double getCellHeight() => 75.w;

  _buildRightTitle() {
    List<Widget> list = [];
    for (int i = 0; i < titleList.length; i++) {
      list.add(_buildTitleContainer(i));
    }
    return list;
  }

  List<Widget> buildRightContent(int dataIndex);

  List<String> getTitleList();


}
